home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13693 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  83 lines

  1. Path: nntphub.cb.att.com!news
  2. From: Aaron Watters <arw@big.att.com>
  3. Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather,comp.lang.python
  4. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  5. Date: Tue, 26 Mar 1996 18:10:33 -0500
  6. Organization: AT&T
  7. Message-ID: <31587969.5259@big.att.com>
  8. References: <Doq3sv.MzA@research.att.com> <1996Mar25.160702.14229@schbbs.mot.com> <4j948d$t3d@trumpet.uni-mannheim.de>
  9. NNTP-Posting-Host: lone.info.att.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
  14. CC: arw@big.att.com
  15.  
  16. Someone way up in this thread wanted to know what errors
  17. were good for other than aborting...
  18.  
  19. Some uses for exceptions, other than as aborts, in python:
  20.  
  21. # get or create a data item from an archive
  22. try:
  23.     person = people[name]
  24. except KeyError:
  25.     person = people[name] = Person(name)
  26.  
  27. # ignore broken urls, but follow the good ones (stolen from a spyder)
  28. urllistpending = [starturl]
  29. goodurls = []
  30. while urllistpending:
  31.     thisurl = urllistpending[-1]  # get/delete last pending
  32.     del urllistpending[-1]
  33.     try:
  34.        text = thisurl.get_text()  # try to follow the url
  35.     except IOError:
  36.        continue # url broken...
  37.     else:
  38.        goodurls.append(thisurl)   # good url, add embedded refs
  39.        newurls = text.urls_mentioned()
  40.        urllistpending = urllistpending + newurls
  41.  
  42. # Do an endless loop, use error to return terminating value
  43. #  from anywhere, even deeply embedded calls (stolen from pickle.py)
  44. try:
  45.     while 1:
  46.         ........ blah blah blah .....
  47. except STOP, value # note: STOP is unique to this module
  48.     result = value
  49.  
  50. # is a path a directory? (even if it doesn't exist?) (from dospath.py)
  51. def isdir(path):
  52.         try:
  53.                 st = os.stat(path)
  54.         except os.error:
  55.                 return 0
  56.         else:
  57.                 return stat.S_ISDIR(st[stat.ST_MODE])
  58.  
  59. # print the object using the .print method, if there is one or use default printing
  60. # otherwise
  61. try:
  62.     print object.print()
  63. except AttributeError:
  64.     print object
  65.  
  66. # and the best of all, if you have a socket module, use it, else use
  67. # the Mac module SOCKS, or the WinSock module, if available
  68. try:
  69.    import socket
  70. except ImportError:
  71.    try:
  72.        import SOCKS
  73.        socket = SOCKS
  74.    except ImportError:
  75.        import WinSock
  76.        socket = WinSock
  77.  
  78. -- Aaron Watters
  79. ====
  80.         "If falling in love is anything like learning how to spell, I don't
  81.  want to  do it.  It takes too long."
  82.      Glenn, age 7 (mailed all over the world, original attribution lost).
  83.